import { defineAsyncComponent, h } from 'vue'
import Loading from '@/components/Loading.vue'
import Error from '@/components/Error.vue'

/**
 * 获取异步加载的页面组件
 *
 * @param path 页面组件的路径
 * @returns 异步加载的页面组件
 */
export const getAsyncPage = (path: string) => {
  return async () => import(path)
}

/**
 * 异步加载组件
 *
 * @param path 组件路径
 * @returns 异步加载的组件
 */
export const getAsyncComponent = (path: string) => {
  return defineAsyncComponent({
    loader: async () => import(path),
    loadingComponent: Loading,
    errorComponent: {
      render() {
        return h(Error, '加载失败')
      },
    },
  })
}
